package com.hsnn.medstgmini.base.std.service.impl;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.base.dict.dao.DicAreaDao;
import com.hsnn.medstgmini.base.dict.dao.DicHosptypeDao;
import com.hsnn.medstgmini.base.dict.model.DicHosptype;
import com.hsnn.medstgmini.base.std.dao.StdHospInfoDao;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.HospInfoLog;
import com.hsnn.medstgmini.base.std.service.StdHospInfoManager;
import com.hsnn.medstgmini.sys.model.RegHospInfo;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 医疗机构库
 * @author CCL
 */
@Service
public class StdHospInfoManagerImpl implements StdHospInfoManager {

	@Autowired
	private StdHospInfoDao hospInfoDao;

	@Autowired
	private DicAreaDao areaDao;

	@Autowired
	private DicHosptypeDao hosptypeDao;

	/**
	 * @category 查询医疗机构列表
	 * @param page
	 * @param hospInfo
	 * @return
	 */
	public Pagination getHospInfoPage(Pagination page, HospInfo hospInfo) {
		return hospInfoDao.getHospInfoPage(page, hospInfo);
	}

	/**
	 * @category 根据id查询医疗机构信息
	 * @param id
	 * @return
	 */
	public HospInfo getHospInfoById(String id) {
		return hospInfoDao.getHospInfoById(id);
	}
	
	/**
	 * @category 根据code查询医疗机构信息
	 * @param code
	 * @return
	 */
	@Override
	public HospInfo getHospInfoByCode(String code) {
		return hospInfoDao.getHospInfoByCode(code);
	}

	/**
	 * @category 保存医疗机构信息
	 * @param hospInfo
	 */
	@Transactional
	public void saveHospInfo(HospInfo hospInfo, int opType) {
		if (hospInfo != null) {
			
			DicHosptype hosptype = hospInfo.getHosptype();
			if (hosptype != null) {
				String id = hosptype.getHospTypeId();
				hospInfo.setHosptype(hosptypeDao.findById(id));
			}
			hospInfoDao.saveHospInfo(hospInfo, opType);
		}
	}

	/**
	 * @category 查询医疗机构维护记录列表
	 * @param page
	 * @param hospInfoLog
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getHospInfoLogPage(Pagination page,HospInfoLog hospInfoLog) {
		page = hospInfoDao.getHospInfoLogPage(page, hospInfoLog);
		List<Map<String,Object>> list = (List<Map<String,Object>>)page.getRows();
		for(int i = 0;i<list.size();i++){
			String detail = list.get(i).get("detail")==null?"":list.get(i).get("detail").toString();
			if(StringUtils.isNotBlank(detail)){
				if(detail.toString().indexOf(",")<0){
					detail = "新增数据:<br/>".concat(detail.replaceAll(";", "<br/>"));
					
				}else{
					detail= detail.replaceAll(":", "由");
					detail= detail.replaceAll(",", "变为");
					detail= detail.replaceAll(";", "<br/>");
				}
			}
			list.get(i).put("detail", detail);
		}
		page.setRows(list);	
		return page;
	}


	/**
	 * @category 批量(启用|停用)
	 * @param list
	 */
	@Transactional
	public void batchChangeStatus(Pagination page) {
		hospInfoDao.batchChangeStatus(page);
	}

	/**
	 * @category 查询维护记录修改前修改后记录
	 * @param hospInfoLog
	 * @return
	 */
	public List<Map<String,Object>> getHospCompareList(HospInfoLog hospInfoLog) {
		return hospInfoDao.getHospCompareList(hospInfoLog);
	}

	/**
	 * @category 校验注册的医疗机构在医疗机构库中是否存在
	 * @date 2015年6月24日
	 * @param hospInfo
	 * @return
	 */
	public boolean checkHospExist(RegHospInfo hospInfo) {
		return hospInfoDao.checkHospExist(hospInfo);
	}

	/**
	 * @category 根据hospCode获取医疗机构信息
	 * @date 2015年7月28日
	 * @param hospInfoCode
	 * @return
	 */
	public List<HospInfo> getHospInfoListByHospCode(String hospCode,String hospId) {
		return hospInfoDao.getHospInfoByHospCode(hospCode,hospId);
	}

	/**
	 * @category 根据hospName获取医疗机构信息
	 * @date 2015年7月28日
	 * @param hospName
	 * @return
	 */
	public List<HospInfo> getHospInfoListByHospName(String hospName,String hospId) {
		return hospInfoDao.getHospInfoByHospName(hospName,hospId);
	}
	
	/**
	 * 根据条件获取不分页数据
	 * @param page
	 * @return page
	 */
	@Override
	public Pagination getStdHospinfoNoPage(Pagination page){
		return hospInfoDao.getStdHospinfoNoPage(page);
	}

	@Override
	public Pagination getHospCount(Pagination page) {
		return hospInfoDao.getHospCount(page);
	}
	public Map<String, Object> getStdHospInfoByCode(String code){
		return hospInfoDao.getStdHospInfoByCode(code);
	}
	public Map<String, Object> getStdHospInfoDetailById(String id){
		return hospInfoDao.getStdHospInfoDetailById(id);
	}
}
